//
//  ZYSportMapViewController.m
//  LoveSport
//
//  Created by 王朝阳 on 2016/11/8.
//  Copyright © 2016年 wangzhaoyang. All rights reserved.
//

#import "ZYSportMapViewController.h"
#import <MAMapKit/MAMapKit.h>
#import "ZYSportTrackingLine.h"
#import "ZYSportCircleAnimation.h"


@interface ZYSportMapViewController ()<MAMapViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *distanceLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@end

@implementation ZYSportMapViewController {
    //是否设置起始位置
    BOOL _hasSetStartLocation;
    ///转场动画
    ZYSportCircleAnimation *_circleAnimation;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setupMap];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        //设置展现样式为自定义
        self.modalPresentationStyle = UIModalPresentationCustom;
        _circleAnimation = [ZYSportCircleAnimation new];
        self.transitioningDelegate = _circleAnimation;
    }
    return self;
}
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation {
    //判断位置是否变化
    if (!updatingLocation) {
        return;
    }
    //判断运动状态是否是继续,如果是绘制运动轨迹
    if (_sportTracking.sportState != ZYSportStateContinue) {
        return;
    }
    //将用户设置为地图视图的中心
    [mapView setCenterCoordinate:userLocation.coordinate animated:YES];
    //判断起始位置是否存在
    if (!_hasSetStartLocation && _sportTracking.sportStartLocation != nil) {
        _hasSetStartLocation = YES;
        //实例化大头针
        MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
        //指定坐标位置
        annotation.coordinate = _sportTracking.sportStartLocation.coordinate;
        //添加到地图上
        [mapView addAnnotation:annotation];
    }
    //绘制运动轨迹
    [mapView addOverlay:[_sportTracking appendLocation:userLocation.location]];
    
    [self updateMapView];
}

- (void)updateMapView {
    _distanceLabel.text = [NSString stringWithFormat:@"%.02f", _sportTracking.totalDistance];
    
}
/**
 单击地图回调,返回经纬度

 @param mapView 地图视图`
 @param coordinate 经纬度
 */
- (void)mapView:(MAMapView *)mapView didSingleTappedAtCoordinate:(CLLocationCoordinate2D)coordinate {
    //开始画线
    

    [self dismissViewControllerAnimated:YES completion:nil];
}
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id<MAOverlay>)overlay {
    //判断 overlay 的类型
    if (![overlay isKindOfClass:[MAPolyline class]]) {
        
        return nil;
    }
    //实例化折线渲染器
//    MAPolylineRenderer *render = [[MAPolylineRenderer alloc] initWithPolyline:overlay];
    ZYSportPolyline *polyline = (ZYSportPolyline *)overlay;
    MAPolylineRenderer *render = [[MAPolylineRenderer alloc] initWithPolyline:polyline];
    //设置属性
    render.lineWidth = 5;
    render.strokeColor = polyline.color;
    return render;
}
//自定义大头针
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation {
    if (![annotation isKindOfClass:[MAPointAnnotation class]]) {
        return nil;
    }
    static NSString *anntationID = @"anntationID";
    MAAnnotationView *anntationView = [mapView dequeueReusableAnnotationViewWithIdentifier:anntationID];
    if (anntationView == nil) {
        anntationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:anntationID];
        
    }
    //设置大头针视图
    UIImage *image = _sportTracking.sportImage;
    anntationView.image = image;
    anntationView.centerOffset = CGPointMake(0, -image.size.height * 0.5);
    return anntationView;
}
/**
 设置地图视图
 */
- (void)setupMap {
    MAMapView *map = [[MAMapView alloc] initWithFrame:self.view.bounds];
//    [self.view addSubview:map];
    //将地图视图添加到最底层
    [self.view insertSubview:map atIndex:0];
    //关闭比例尺
    map.showsScale = NO;
    //关闭相机旋转
    map.rotateCameraEnabled = NO;
    //显示用户位置
    map.showsUserLocation = YES;
    //跟踪用户位置
    map.userTrackingMode = MAUserTrackingModeFollowWithHeading;
    //允许后台定位
    map.allowsBackgroundLocationUpdates = YES;
    //不允许系统暂停位置更新
    map.pausesLocationUpdatesAutomatically = NO;
    //设置代理
    map.delegate = self;
    //记录地图视图
    _mapView = map;
}
@end
